package com.junqson.je.content.service;

import com.junqson.je.common.exception.BizException;
import com.junqson.je.common.model.vo.SimpleUserInfo;
import com.junqson.je.common.util.AppUtil;
import com.junqson.je.content.model.PostContentModel;
import com.junqson.je.content.model.PostModel;
import com.junqson.je.content.model.dto.PostDTO;
import com.junqson.je.content.model.vo.ColumnWithTotal;
import com.junqson.je.content.model.vo.PostContentVo;
import com.junqson.je.content.model.vo.PostListVo;
import com.junqson.je.content.repo.PostContentRepo;
import com.junqson.je.content.repo.PostRepo;
import com.junqson.je.content.util.PageUtil;
import com.junqson.je.content.util.UserInfoCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Descriptions
 * @Company
 * @Author Junqson
 * @Date 2020/2/11 8:46
 * @Version 1.0
 */
@Service
@Slf4j
public class PostService {
    @Autowired
    PostRepo postRepo;

    @Autowired
    PostContentRepo postContentRepo;

    @Autowired
    CommentService commentService;

    @Autowired
    UserInfoCache userInfoCache;

    /**
     * 发帖
     * @param postDTO
     * @param userId
     */
    @Transactional
    public void addPost(PostDTO postDTO, Integer userId) {
        PostModel postModel = new PostModel();
        postModel.setTitle(postDTO.getTitle());
        postModel.setSection(postDTO.getSection());
        postModel.setUserId(userId);

        postRepo.save(postModel);

        PostContentModel contentModel = new PostContentModel();
        contentModel.setPostId(postModel.getPostId());
        contentModel.setContent(postDTO.getContent());

        postContentRepo.save(contentModel);
    }

    /**
     * 删帖
     */
    @Transactional
    public void deletePost(PostModel postModel) {
        postRepo.delete(postModel);
        postContentRepo.deleteById(postModel.getPostId());
        commentService.deleteByPostId(postModel.getPostId());
    }


    /**
     * 分页查询帖子列表
     * @param section
     * @param page
     * @param size
     * @return
     */
    public ColumnWithTotal pagePostBySection(String section, Integer page, Integer size) {
        Sort sort = Sort.by(Sort.Direction.DESC, "updateTime");
        Pageable pageable = PageUtil.getPageable(page-1, size, sort);
        Page<PostModel> models = postRepo.findBySection(section, pageable);
        ColumnWithTotal columnWithTotal = new ColumnWithTotal();
        columnWithTotal.setPostList(models.getContent().stream().map(this::convertToCPostListVo).collect(Collectors.toList()));
        columnWithTotal.setTotal(models.getTotalPages()*size);
        return columnWithTotal;
    }

    /**
     * 获取帖子详情
     * @param postId
     * @return
     */
    public PostContentVo getPostContent(Long postId) {
        PostModel postModel = postRepo.findById(postId).orElseThrow(() -> new BizException("帖子不存在"));
        PostContentModel contentModel = postContentRepo.findById(postId).orElseThrow(() -> new BizException("帖子内容不存在"));
        PostContentVo vo = new PostContentVo();

        SimpleUserInfo publisher = userInfoCache.getUserInfo(postModel.getUserId());
        AppUtil.copyPropertiesIgnoreNull(postModel, vo);
        vo.setUserInfo(publisher);
        vo.setContent(contentModel.getContent());
        long commentCount = commentService.countCommentByPostId(postId);
        vo.setCommentCount(commentCount);

        return vo;
    }


    public PostModel getPostModelById(Long postId) {
        return postRepo.findById(postId).orElseThrow(() -> new BizException("帖子不存在"));
    }

    public PostListVo convertToCPostListVo(PostModel postModel) {
        if (postModel == null) {
            return null;
        }
        PostListVo postListVo = new PostListVo();
        AppUtil.copyPropertiesIgnoreNull(postModel, postListVo);
        SimpleUserInfo publisher = userInfoCache.getUserInfo(postModel.getUserId());
        postListVo.setUserInfo(publisher);
        return postListVo;
    }

}
